﻿package mortal.game.scene3D.ai.ais
{
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.game.scene3D.player.entity.*;

    public class FollowAI extends MoveByPathAI
    {
        protected var _player:MovePlayer;
        protected var _nextMoveTime:Number = 0;
        protected var _moveSplitTime:int = 400;

        public function FollowAI()
        {
            return;
        }// end function

        override public function start(param1:Function = null) : void
        {
            this._player = _data.target as MovePlayer;
            _meRole = _data.meRole;
            _callback = param1;
            if (this._player == null)
            {
                super.stop();
                return;
            }
            this.updateTilePoint();
            updatePath();
            this._player.pointChangeHandler = this.onTargetChangeHandler;
            this._player.realPointChangeHandler = this.onTargetChangeHandler;
            _data.params = this._paths;
            super.start(param1);
            return;
        }// end function

        override public function stop(param1:Boolean = true) : void
        {
            if (this._player != null)
            {
                this._player.pointChangeHandler = null;
                this._player.realPointChangeHandler = null;
            }
            super.stop(param1);
            return;
        }// end function

        public function onTargetChangeHandler() : void
        {
            var _loc_1:* = getTimer();
            if (_loc_1 < this._nextMoveTime)
            {
                return;
            }
            this._nextMoveTime = _loc_1 + this._moveSplitTime;
            this.updateTilePoint();
            updatePath();
            sceneMove();
            return;
        }// end function

        override protected function updateTilePoint() : void
        {
            if (_targetPixPoint == null)
            {
                _targetPixPoint = new Point();
            }
            _targetPixPoint.x = this._player.realX2d;
            _targetPixPoint.y = this._player.realY2d;
            return;
        }// end function

        override protected function onWalkEnd(event:Event) : void
        {
            if (isInRange())
            {
                if (_meRole.isMove)
                {
                    _meRole.stopWalking();
                }
                this.stop();
            }
            else
            {
                sceneMove();
            }
            return;
        }// end function

        override protected function onGirdWalkEnd(event:Event) : void
        {
            if (isInRange())
            {
                if (_meRole.isMove)
                {
                    _meRole.stopWalking();
                }
                this.stop();
            }
            return;
        }// end function

    }
}
